package com.pbxadmin.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.google.gson.Gson;
import com.pbxadmin.cfg.WSConstants;
import com.pbxadmin.pojo.Route;
import com.pbxadmin.service.PbxWebService;

@Controller
@RequestMapping("/route")
public class RouteController {

	private Logger log = LoggerFactory.getLogger(RouteController.class);

	@Autowired
	private PbxWebService pbxWebService;
	@Autowired
	private Gson gson;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		//
		String returnCode = pbxWebService.invokePbx("list_route");
		if (returnCode.isEmpty()) {
			return new ModelAndView("/error/pbxerror", "message", "pbxservice 发生错误，请稍候再试。");
		}
		Map<String,Route> mapRoute = gson.fromJson(returnCode, WSConstants.mapRoute);
		//
		ModelAndView mav = new ModelAndView("/route/list");
		mav.addObject("mapRoute",mapRoute);
		return mav;
	}

	/**
	 * 详情
	 */
	@RequestMapping("/detail/{callee}")
	public ModelAndView detail(HttpServletRequest req, @PathVariable("callee") String callee) {
		//
		if (callee == null || callee.isEmpty()) {
			return new ModelAndView("/error/parameters", "message", "/route/detail/{callee} , callee is null");
		}
		//
		String returnCode = pbxWebService.invokePbx("list_route");
		Map<String,Route> mapRoute = gson.fromJson(returnCode, WSConstants.mapRoute);
		Route route = mapRoute.get(callee);

		ModelAndView mav = new ModelAndView("/route");
		
		return mav;
	}

	/**
	 * 修改初始化
	 */
	@RequestMapping("/modify/{callee}/init")
	public ModelAndView modifyInit(HttpServletRequest req, @PathVariable("callee") String callee) {
		//
		if (callee == null || callee.isEmpty()) {
			return new ModelAndView("/error/parameters", "message", "parameter is null");
		}
		//
		String result = req.getParameter("result");
		if (result != null) {
			result = new String(Base64.decodeBase64(result));
		} else {
			result = "";
		}
		//
		String returnCode = pbxWebService.invokePbx("list_route");
		Map<String,Route> mapRoute = gson.fromJson(returnCode, WSConstants.mapRoute);
		Route route = mapRoute.get(callee);
		//
		ModelAndView mav = new ModelAndView("/route/modify");
		mav.addObject("route", route);
		mav.addObject("callee", callee);
		mav.addObject("result", result);
		return mav;
	}

	/**
	 * 修改
	 */
	@RequestMapping("/modify/{callee}")
	public ModelAndView modify(HttpServletRequest req, @PathVariable("callee")
	String callee, @ModelAttribute Route route) {
		if (callee == null || callee.isEmpty()) {
			return new ModelAndView("/error/parameters", "message", "/route/detail/{callee} , callee is null");
		}

		String gsonStr = gson.toJson(route);
		String returnCode = pbxWebService.invokePbx("mod_route", new Object[] { callee, gsonStr });
		String result = "";
		if (WSConstants.RTN_SUCCESS.equals(returnCode)) {
			result = "修改路由[" + callee + "]成功";
		} else {
			result = "修改路由[" + callee + "]失败";
		}
		result = Base64.encodeBase64URLSafeString(result.getBytes());
		return new ModelAndView(new RedirectView("/pbxadmin/route/modify/" + callee + "/init"), "result", result);
	}

	public PbxWebService getPbxWebService() {
		return pbxWebService;
	}

	public void setPbxWebService(PbxWebService pbxWebService) {
		this.pbxWebService = pbxWebService;
	}

	public Gson getGson() {
		return gson;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}
}
